﻿Module Bootstrapping
    ''' <summary>
    ''' Process every command line specified at launch time
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ProcessCommandLineParameters()
        If My.Application.CommandLineArgs.Count = 0 Then Exit Sub

        Dim i As Integer
        For i = 0 To My.Application.CommandLineArgs.Count - 1
            Dim parameter As String
            parameter = My.Application.CommandLineArgs(i)
            parameter = Trim(parameter)
            If parameter Like "/*=*" Then
                ' Parameter is a Modifier
                Dim command As String
                Dim value As String
                Dim tmpSplitted() As String
                tmpSplitted = Split(parameter, "=")
                If Not tmpSplitted.Length = 2 Then
                    AddLog(" Invalid parameter <" & parameter & "> specified.")
                    GoTo NextLoop
                End If
                command = tmpSplitted(0)
                value = tmpSplitted(1)
                command = Right(command, command.Length - 1) ' Remove the head '/'
                command = LCase(command)
                command = Trim(command)
                value = Trim(value)
                If command = "" Then
                    AddLog(" Invalid parameter <" & parameter & "> specified.")
                    GoTo NextLoop
                End If
                If value = "" Then
                    AddLog(" Invalid parameter <" & parameter & "> specified.")
                    GoTo NextLoop
                End If
                CheckModifierParameter(command, value)
            ElseIf parameter Like "/*" Then
                ' Parameter is a Switch
                Dim switch As String
                switch = parameter
                switch = Trim(switch)
                switch = Right(switch, switch.Length - 1) ' Remove the head '/'
                CheckSwitchParameter(switch)
            Else
                ' Parameter is a filename
                Dim command As String
                Dim value As String
                If Configuration.CurrentConfiguration.InputFilename Is Nothing Then
                    command = "include"
                Else
                    command = "output"
                End If
                value = parameter
                CheckModifierParameter(command, value)
            End If
NextLoop:
        Next
    End Sub
    ''' <summary>
    ''' Checks and applies a Switch parameter (/something)
    ''' </summary>
    ''' <param name="switch">Switch to be applied</param>
    ''' <remarks></remarks>
    Public Sub CheckSwitchParameter(ByVal switch As String)
        Select Case switch
            Case "verbose", "v"
                CurrentConfiguration.Verbose = True
            Case "help", "h", "?"
                CurrentConfiguration.JumpToShell = False
                CurrentConfiguration.PrintHelpOnly = True
            Case "auto", "a"
                CurrentConfiguration.JumpToShell = False
            Case Else
                AddLog("Unrecognized switch <" & switch & ">")
                Exit Sub
        End Select
    End Sub
    ''' <summary>
    ''' Checks and applies a Modifier parameter (/something=value)
    ''' </summary>
    ''' <param name="command">Command that represents a value that will be changed</param>
    ''' <param name="parameter">The new value of command</param>
    ''' <remarks></remarks>
    Public Sub CheckModifierParameter(ByVal command As String, ByVal parameter As String)
        Select Case command
            Case "include", "inc", "i"
                ' Adds a source file to the compilation queue
                Configuration.CurrentConfiguration.InputFilename.Add(parameter)
            Case "output", "out", "o"
                ' Specified the output file
                If Not Configuration.CurrentConfiguration.OutputFilename = "" Then
                    AddLog("Multiple output files defined, overriding the previous one...")
                End If
                Configuration.CurrentConfiguration.OutputFilename = parameter
            Case "protocol", "prt", "p"
                ' Specified the protocol used
                If Not Configuration.CurrentConfiguration.OutputProtocol = "" Then
                    AddLog("Multiple output files defined, overriding the previous one...")
                End If
                Configuration.CurrentConfiguration.OutputProtocol = parameter
            Case "root", "r"
                ' Specifies the working path of the application
                Dim di As New IO.DirectoryInfo(parameter)
                If di.Exists = False Then
                    AddLog("Cant set root directory to <" & parameter & ">.")
                    AddLog("Missing directory.")
                Else
                    IO.Directory.SetCurrentDirectory(di.FullName)
                    AddLog("New root directory successfully set.")
                    AddLog(" :" & di.FullName)
                End If
            Case "pattern", "ipa", "s", "patternrecursive", "ipr", "sr" ' The first three are for Non-Recursive operations, the last three recurses
                ' Include by Pattern
                Dim recursive As Boolean
                Select Case command
                    Case "patternrecursive", "ipr", "sr" ' Only recursives here
                        recursive = True
                    Case Else
                        recursive = False
                End Select
                Try
                    Dim pattern As String
                    pattern = parameter
                    Dim filenames() As String
                    Dim path As String
                    Dim components() As String = Split(pattern, "\")
                    If components.Length = 0 Then
                        AddLog("Invalid search pattern specified.")
                        Exit Try
                    End If
                    If components.Length = 1 Then
                        ' Only a pattern specified
                        pattern = components(0)
                        If Not recursive Then
                            filenames = IO.Directory.GetFiles(IO.Directory.GetCurrentDirectory, pattern, IO.SearchOption.TopDirectoryOnly)
                        Else
                            filenames = IO.Directory.GetFiles(IO.Directory.GetCurrentDirectory, pattern, IO.SearchOption.AllDirectories)
                        End If
                    Else
                        ' Path + Pattern specified
                        path = ConsolidateParameters(components, 0, components.Length - 2, "\")
                        pattern = components(components.Length - 1)
                        Dim di As New IO.DirectoryInfo(path)
                        If di.Exists = False Then
                            AddLog("Specified directory not found.")
                            Exit Try
                        End If
                        path = di.FullName
                        If Not recursive Then
                            filenames = IO.Directory.GetFiles(path, pattern, IO.SearchOption.TopDirectoryOnly)
                        Else
                            filenames = IO.Directory.GetFiles(path, pattern, IO.SearchOption.AllDirectories)
                        End If

                    End If

                    If filenames Is Nothing Then
                        AddLog("No files found.")
                        Exit Try
                    End If
                    If filenames.Length = 0 Then
                        AddLog("No files found.")
                        Exit Try
                    End If

                    Dim i As Integer
                    For i = 0 To filenames.Length - 1
                        Dim file As String = filenames(i)
                        file = Trim(file)
                        If Not file = "" Then
                            CurrentConfiguration.InputFilename.Add(file)
                        End If
                    Next

                    AddLog(filenames.Length & " files found and added.")
                Catch ex As Exception
                    AddLog("Exception found while performing include operation!")
                    AddLog("   EX: " & ex.Message)
                End Try
            Case Else
                AddLog("Unrecognized modifier <" & command & ">")
                Exit Sub
        End Select
    End Sub

    ''' <summary>
    ''' Prints help about the commands supported by the App or the Shell
    ''' </summary>
    ''' <param name="externalHelp">True to print only command line help, False to print internal Shell commands help</param>
    ''' <remarks></remarks>
    Public Sub PrintHelp(Optional ByVal externalHelp As Boolean = False)
        AddLog("===========")
        AddLog(" Help Page")
        AddLog("===========")
        AddLog()
        If externalHelp Then
            ' Application Main Help
            AddLog("No External help available yet!. Hey dev! write some code here!")
        Else
            ' Internal Shell Main Help
            AddLog("No Internal help available yet!. Hey dev! write some code here!")
        End If
    End Sub
End Module
